package com.zlm.linked;

import java.util.Objects;

/**
 * @author zlm
 * @date 2025/4/29
 */
public class DeleteNode {

    public static void main(String[] args) {

        Node<Integer> head = new Node<>(1, null);
        head.next = new Node<>(2, null);
        head.next.next = new Node<>(3, null);
        head.next.next.next = new Node<>(4, null);
        head.next.next.next.next = new Node<>(5, null);
        head.next.next.next.next.next = new Node<>(6, null);

        head = deleteNode(head, 6);
        head.printNode();

    }

    /**
     * 删除链表中指定的值
     * @param head
     * @param value
     * @return
     * @param <E>
     */
    public static <E> Node<E> deleteNode(Node<E> head, E value) {
        if(head == null){
            return head;
        }
        //虚拟头节点
        Node<E> tmpHead = new Node<>(null,null);
        tmpHead.next = head;
        //指针
        Node<E> current = head;

        while (current.next != null){
            if(Objects.equals(value, current.next.value)){
                current.next = current.next.next;
            }else{
                current = current.next;
            }
        }
        return tmpHead.next;
    }
}
